part of 'fragment.dart';

// ignore: camel_case_types
class _PurchaseManagementFragmentView_900_infinity extends StatelessWidget {
  const _PurchaseManagementFragmentView_900_infinity({
    required _PurchaseManagementFragmentWloc wloc,
  }) : _wloc = wloc;

  final _PurchaseManagementFragmentWloc _wloc;

  Widget _buildContent({
    required _PurchaseManagementFragmentWlocState state,
    required BuildContext context,
  }) {
    if (state is _InitialPurchaseManagementFragmentWlocState) {
      return _buildInitialStateContent(
        state: state,
        context: context,
      );
    }

    if (state is _LoadingPurchaseManagementFragmentWlocState) {
      return _buildLoadingStateContent(
        state: state,
        context: context,
      );
    }

    if (state is _LoadedPurchaseManagementFragmentWlocState) {
      return _buildLoadedStateContent(
        state: state,
        context: context,
      );
    }

    throw UnimplementedError();
  }

  Widget _buildInitialStateContent({
    required _InitialPurchaseManagementFragmentWlocState state,
    required BuildContext context,
  }) {
    return const SizedBox();
  }

  Widget _buildLoadingStateContent({
    required _LoadingPurchaseManagementFragmentWlocState state,
    required BuildContext context,
  }) {
    return const DelayedWidget(
      delay: Duration(milliseconds: 500),
      child: Center(
        child: AppLoader(),
      ),
    );
  }

  // ignore: non_constant_identifier_names
  Widget _buildLoadedStateContent({
    required _LoadedPurchaseManagementFragmentWlocState state,
    required BuildContext context,
  }) {
    final widgets = [
      const SizedBox(
        height: 48,
      ),
      _buildTitle(
        context: context,
      ),
    ];

    if (state.activePurchasedItemModels.isNotEmpty) {
      widgets.addAll([
        const SizedBox(
          height: 36,
        ),
        const _Divider(),
        const SizedBox(
          height: 36,
        ),
        Text(
          PurchaseManagementFragmentTranslation.of(context).activePurchasedItemTileGroupHeaderTitle,
          style: PurchaseManagementFragmentTheme.of(context).textTheme.tileGroupHeaderTitle,
        ),
        const SizedBox(
          height: 36,
        ),
      ]);

      for (var i = 0; i < state.activePurchasedItemModels.length; i++) {
        if (i > 0) {
          widgets.add(
            const SizedBox(
              height: 20,
            ),
          );
        }

        widgets.add(_buildActivePurchasedItemTile(
          purchasedItemModel: state.activePurchasedItemModels[i],
          context: context,
        ));
      }
    }

    if (state.pausedGooglePlayPurchasedSubscriptionModels.isNotEmpty) {
      widgets.addAll([
        const SizedBox(
          height: 36,
        ),
        const _Divider(),
        const SizedBox(
          height: 36,
        ),
        Text(
          PurchaseManagementFragmentTranslation.of(context).pausedPurchasedItemTileGroupHeaderTitle,
          style: PurchaseManagementFragmentTheme.of(context).textTheme.tileGroupHeaderTitle,
        ),
        const SizedBox(
          height: 36,
        ),
      ]);

      for (var i = 0; i < state.pausedGooglePlayPurchasedSubscriptionModels.length; i++) {
        if (i > 0) {
          widgets.add(
            const SizedBox(
              height: 20,
            ),
          );
        }

        widgets.add(_buildPausedGooglePlayPurchasedSubscriptionTile(
          googlePlayPurchasedSubscriptionModel:
              state.pausedGooglePlayPurchasedSubscriptionModels[i],
          context: context,
        ));
      }
    }

    if (state.expiredPurchasedSubscriptionModels.isNotEmpty) {
      widgets.addAll([
        const SizedBox(
          height: 36,
        ),
        const _Divider(),
        const SizedBox(
          height: 36,
        ),
        Text(
          PurchaseManagementFragmentTranslation.of(context)
              .expiredPurchasedItemTileGroupHeaderTitle,
          style: PurchaseManagementFragmentTheme.of(context).textTheme.tileGroupHeaderTitle,
        ),
        const SizedBox(
          height: 36,
        ),
      ]);

      for (var i = 0; i < state.expiredPurchasedSubscriptionModels.length; i++) {
        if (i > 0) {
          widgets.add(
            const SizedBox(
              height: 20,
            ),
          );
        }

        widgets.add(_buildExpiredPurchasedSubscriptionTile(
          purchasedSubscriptionModel: state.expiredPurchasedSubscriptionModels[i],
          context: context,
        ));
      }
    }

    widgets.add(const SizedBox(
      height: 48,
    ));

    return Row(
      children: [
        const SizedBox(
          width: 100,
        ),
        SizedBox(
          width: 700,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: widgets,
          ),
        ),
        const SizedBox(
          width: 100,
        ),
        const Spacer(),
      ],
    );
  }

  Widget _buildTitle({
    required BuildContext context,
  }) {
    return buildMarkdown(
      markdown: PurchaseManagementFragmentTranslation.of(context).titleMarkdown,
      textStyle: PurchaseManagementFragmentTheme.of(context).textTheme.title,
      textAlign: TextAlign.start,
      context: context,
      markedTextStyle: TextStyle(
        color: CoreTheme.of(context).primaryColor,
      ),
    );
  }

  Widget _buildTile({
    required String iconAssetPath,
    required String title,
    required BuildContext context,
    String? priceLabel,
    String? timeLabel,
    Widget? button,
  }) {
    final textWidgets = <Widget>[
      Text(
        title,
        style: PurchaseManagementFragmentTheme.of(context).textTheme.tileTitle,
        textWidthBasis: TextWidthBasis.longestLine,
        maxLines: 1,
      ),
    ];

    if (priceLabel != null) {
      textWidgets.addAll([
        const SizedBox(
          height: 4,
        ),
        Text(
          priceLabel,
          style: PurchaseManagementFragmentTheme.of(context).textTheme.tilePriceLabel,
          textWidthBasis: TextWidthBasis.longestLine,
          maxLines: 1,
        ),
      ]);
    }

    if (timeLabel != null) {
      textWidgets.addAll([
        const SizedBox(
          height: 4,
        ),
        Text(
          timeLabel,
          style: PurchaseManagementFragmentTheme.of(context).textTheme.tileTimeLabel,
          textWidthBasis: TextWidthBasis.longestLine,
          maxLines: 1,
        ),
      ]);
    }

    return Row(
      children: [
        Image.asset(
          iconAssetPath,
          width: 65,
          height: 65,
          filterQuality: FilterQuality.high,
        ),
        const SizedBox(
          width: 16,
        ),
        Flexible(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: textWidgets,
          ),
        ),
        if (button != null) ...[
          const SizedBox(
            width: 16,
          ),
          button,
        ],
      ],
    );
  }

  Widget _buildActivePurchasedItemTile({
    required PurchasedItemModel purchasedItemModel,
    required BuildContext context,
  }) {
    final iconAssetPath = _wloc.getPurchasedItemIconAssetPath(
      purchasedItemModel: purchasedItemModel,
    );

    final title = _wloc.getPurchasedItemTitle(
      purchasedItemModel: purchasedItemModel,
    );

    String? priceLabel;
    Widget? button;

    if (purchasedItemModel is WebPurchasedSubscriptionModel) {
      final webPurchasedSubscriptionModel = purchasedItemModel;

      priceLabel = _wloc.getWebPurchasedSubscriptionPriceLabel(
        price: webPurchasedSubscriptionModel.pretifiedPrice,
        currency: webPurchasedSubscriptionModel.currency,
        duration: webPurchasedSubscriptionModel.duration,
      );

      final String buttonTitle;
      final VoidCallback onPressed;

      if (webPurchasedSubscriptionModel.autoRenewalEnabled) {
        buttonTitle =
            PurchaseManagementFragmentTranslation.of(context).subscriptionCancellationButtonTitle;
        onPressed = () {
          _wloc.onWebPurchasedSubscriptionCancellationButtonPressed(
            originalTransactionId: webPurchasedSubscriptionModel.originalTransactionId,
          );
        };
      } else {
        buttonTitle = PurchaseManagementFragmentTranslation.of(context)
            .subscriptionAutoRenewalEnablementButtonTitle;
        onPressed = () {
          _wloc.onWebPurchasedSubscriptionAutoRenewalEnablementButtonPressed(
            originalTransactionId: webPurchasedSubscriptionModel.originalTransactionId,
          );
        };
      }

      button = _TileButton(
        title: buttonTitle,
        onPressed: onPressed,
      );
    }

    String? timeLabel;

    if (purchasedItemModel is PurchasedSubscriptionModel) {
      final purchasedSubscriptionModel = purchasedItemModel;

      if (purchasedSubscriptionModel.autoRenewalEnabled) {
        timeLabel = PurchaseManagementFragmentTranslation.of(context)
            .buildActivePurchasedSubscriptionRenewalLabel(
          autoRenewalTime: purchasedSubscriptionModel.expirationTime!,
        );
      } else {
        timeLabel = PurchaseManagementFragmentTranslation.of(context)
            .buildActivePurchasedSubscriptionExpirationLabel(
          expirationTime: purchasedSubscriptionModel.expirationTime!,
        );
      }
    }

    return _buildTile(
      iconAssetPath: iconAssetPath,
      title: title,
      priceLabel: priceLabel,
      timeLabel: timeLabel,
      button: button,
      context: context,
    );
  }

  Widget _buildPausedGooglePlayPurchasedSubscriptionTile({
    required GooglePlayPurchasedSubscriptionModel googlePlayPurchasedSubscriptionModel,
    required BuildContext context,
  }) {
    final iconAssetPath = _wloc.getPurchasedItemIconAssetPath(
      purchasedItemModel: googlePlayPurchasedSubscriptionModel,
    );

    final title = _wloc.getPurchasedItemTitle(
      purchasedItemModel: googlePlayPurchasedSubscriptionModel,
    );

    final timeLabel = PurchaseManagementFragmentTranslation.of(context).buildResumeLabel(
      autoResumeTime: googlePlayPurchasedSubscriptionModel.pauseInfo!.autoResumeTime,
    );

    return _buildTile(
      iconAssetPath: iconAssetPath,
      title: title,
      timeLabel: timeLabel,
      context: context,
    );
  }

  Widget _buildExpiredPurchasedSubscriptionTile({
    required PurchasedSubscriptionModel purchasedSubscriptionModel,
    required BuildContext context,
  }) {
    final iconAssetPath = _wloc.getPurchasedItemIconAssetPath(
      purchasedItemModel: purchasedSubscriptionModel,
    );

    final title = _wloc.getPurchasedItemTitle(
      purchasedItemModel: purchasedSubscriptionModel,
    );

    final timeLabel = PurchaseManagementFragmentTranslation.of(context)
        .buildExpiredPurchasedSubscriptionExpirationLabel(
      expirationTime: purchasedSubscriptionModel.expirationTime!,
    );

    return _buildTile(
      iconAssetPath: iconAssetPath,
      title: title,
      timeLabel: timeLabel,
      context: context,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: CoreTheme.of(context).backgroundColor,
      child: WlocBuilder<_PurchaseManagementFragmentWloc, _PurchaseManagementFragmentWlocState>(
        wloc: _wloc,
        builder: (context, state) {
          return _buildContent(
            state: state,
            context: context,
          );
        },
      ),
    );
  }
}
